package com.itheima.gen;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class TestCaseGenerator {

    public static String list(int size,
                              Function<Integer, String[]> generator) {
        return list(size, generator, null);
    }

    public static String list(int size,
                              Function<Integer, String[]> generator,
                              Function<String[], String> toString) {
        final String prefix = "Object[][] data = new Object[][]{" + System.lineSeparator();
        final String suffix = System.lineSeparator() + "};";
        StringJoiner joiner = new StringJoiner(", " + System.lineSeparator(), prefix, suffix);
        for (int i = 0; i < size; i++) {
            final String[] items = generator.apply(i);
            joiner.add(toString == null ? String.format("\t{%s}", String.join(", ",items)): toString.apply(items));
        }
        return joiner.toString();
    }

    public static <K, V> String map(int size, Function<Integer, K> getKey, Function<K, V> getValue) {
        Map<K, V> testCases = new LinkedHashMap<>();
        String keyType = "String";
        String valueType = "String";
        for (int i = 0; i < size; i++) {
            // 随机生成一个日期字符串
            K key = getKey.apply(i);
            V value = getValue.apply(key);
            testCases.put(key, value);

            // 获取key和value的类型
            if (i == 0) {
                keyType = key.getClass().getSimpleName();
                if (value != null) {
                    valueType = value.getClass().getSimpleName();
                }
            }
        }
        
        boolean isValueString = Objects.equals(valueType, "String");

        String finalKeyType = keyType;
        final Function<Map.Entry<K, V>, String> mapper = e ->
                String.format("%s, %s",
                        getKeyString(finalKeyType, e),
                        e.getValue() != null ? (isValueString ? ("\"" + e.getValue() + "\"") : e.getValue()) : null
                );

        if (testCases.size() <= 10) {
            return testCases.entrySet().stream()
                    .map(mapper)
                    .map(s -> "\t" + s)
                    .collect(
                            Collectors.joining("," + System.lineSeparator(),
                                    "Map<" + keyType + ", " + valueType + "> testCases = Map.of(\r\n", System.lineSeparator() + ");")
                    );
        }

        final StringBuilder resultBuilder = new StringBuilder(String.format("Map<%s, %s> testCases = new LinkedHashMap<>();\r\n", keyType, valueType));
        final String data = testCases.entrySet().stream()
                .map(mapper)
                .map(s -> String.format("testCases.put(%s);", s))
                .collect(Collectors.joining(System.lineSeparator()));
        return resultBuilder.append(data).toString();
    }

    private static <K, V> Object getKeyString(String keyType, Map.Entry<K, V> e) {
        boolean isKeyString = Objects.equals(keyType, "String");
        return isKeyString ? "\"" + e.getKey() + "\"" : e.getKey();
    }

    public static void main(String[] args) {
        // // 生成随机的日期字符串，格式类似于2022-12-11
        final Function<Integer, String> f = index -> String.format("%d-%02d-%02d", 2000 + (int) (Math.random() * 100), 1 + (int) (Math.random() * 12), 1 + (int) (Math.random() * 28));

        System.out.println(map(10, f, key -> Generator.number(1, 7)));
        System.out.println(map(11, f, key -> Generator.number(1, 7)));
    }

}
